/*
 * Copyright (c) 2015 张涛.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kymjs.contacts.widget;


import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;

/**
 * 侧边栏
 */
public class SideBar extends Component implements Component.DrawTask, Component.TouchEventListener {
    // 触摸事件
    private OnTouchingLetterChangedListener onTouchingLetterChangedListener;
    // 26个字母
    static String[] b = {"#", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
            "M", "N", "O", "P", "Q",
            "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    private int choose = -1;// 选中
    private Paint paint = new Paint();

    private Text mTextDialog;

    public void setTextView(Text mTextDialog) {
        this.mTextDialog = mTextDialog;
    }

    public SideBar(Context context, AttrSet attrs, String defStyle) {
        super(context, attrs, defStyle);
        setTouchEventListener(this);
        invalidate();
    }

    public SideBar(Context context, AttrSet attrs) {
        this(context, attrs,"");
    }

    public SideBar(Context context) {
        super(context);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        // 获取焦点改变背景颜色.
        int height = getHeight();// 获取对应高度
        int width = getWidth(); // 获取对应宽度

        float singleHeight = (height * 1f) / b.length;// 获取每一个字母的高度
        singleHeight = (height * 1f - singleHeight / 2) / b.length;
        for (int i = 0; i < b.length; i++) {
            paint.setColor(new Color(Color.rgb(86, 86, 86)));
            paint.setAntiAlias(true);
            paint.setTextSize(23);

            // x坐标等于中间-字符串宽度的一半.
            float xPos = width / 2f - paint.measureText(b[i]) / 2;
            float yPos = singleHeight * i + singleHeight;
            canvas.drawText(paint,b[i], xPos, yPos );
            paint.reset();// 重置画笔
        }
    }

    @Override
    public void invalidate() {
        super.invalidate();
        addDrawTask(this::onDraw);
    }

    /**
     * 向外公开的方法
     *
     * @param onTouchingLetterChangedListener
     */
    public void setOnTouchingLetterChangedListener(OnTouchingLetterChangedListener
                                                           onTouchingLetterChangedListener) {
        this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        final int action = touchEvent.getAction();
        MmiPoint point = touchEvent.getPointerPosition(touchEvent.getIndex());
        final float y = point.getY();// 点击y坐标
        final int oldChoose = choose;
        final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
        final int c = (int) (y / getHeight() * b.length);// 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.

        switch (action) {
            case TouchEvent.PRIMARY_POINT_UP:
                ShapeElement shapeElement = new ShapeElement();
                shapeElement.setRgbColor(new RgbColor(255,255,255,0));
                setBackground(shapeElement);
                choose = -1;//
                invalidate();
                if (mTextDialog != null) {
                    mTextDialog.setVisibility(INVISIBLE);
                }
                break;
            default:
                ShapeElement shapeElement2 = new ShapeElement();
                shapeElement2.setRgbColor(new RgbColor(233,232,233));
                setBackground(shapeElement2);
                if (oldChoose != c) {
                    if (c >= 0 && c < b.length) {
                        if (listener != null) {
                            listener.onTouchingLetterChanged(b[c]);
                        }
                        if (mTextDialog != null) {
                            mTextDialog.setText(b[c]);
                            mTextDialog.setVisibility(VISIBLE);
                        }
                        choose = c;
                        invalidate();
                    }
                }

                break;
        }
        return true;
    }


    /**
     * 接口
     *
     * @author coder
     */
    public interface OnTouchingLetterChangedListener {
        void onTouchingLetterChanged(String s);
    }
}